<template>
  <el-dialog v-loading="loading" :visible.sync="visible" :title="'选择官方模板'" :close-on-click-modal="false"
    :close-on-press-escape="false" :append-to-body="true" :width="'70%'">
    <div>
      <el-form :inline="true" :model="dataForm" @submit.native.prevent @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.title" :placeholder="$t('wechatTemplate.title')" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchEvent()">{{ $t('query') }}</el-button>
          <el-button @click="searchReset()">{{ $t('reset') }}</el-button>
          <el-button type="danger" @click="syncEvent()">{{ '同步官方' }}</el-button>
        </el-form-item>
      </el-form>
      <div class="mod-wechat__wechatTemplate">
        <vxe-table :loading="dataListLoading" :data="dataList" ref="xTable" auto-resize class="vxe-table-element"
          show-overflow="title" remote-sort @sort-change="dataListSortChangeHandle" @cell-dblclick="cellDblclick">
          <!-- <vxe-table-column type="index" width="50" align="center"></vxe-table-column> -->
          <!-- <vxe-table-column field="accountId" title="wechatTemplate.accountId"></vxe-table-column> -->
          <vxe-table-column field="templateIdWx" title="wechatTemplate.templateIdWx"></vxe-table-column>
          <vxe-table-column field="title" title="wechatTemplate.title"></vxe-table-column>
          <vxe-table-column field="primaryIndustry" title="wechatTemplate.primaryIndustry" width="150">
          </vxe-table-column>
          <vxe-table-column field="deputyIndustry" title="wechatTemplate.deputyIndustry" width="150"></vxe-table-column>
          <vxe-table-column field="example" title="wechatTemplate.example"></vxe-table-column>
        </vxe-table>
        <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
        </el-pagination>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      multiple: false,
      loading: false,
      visible: false,
      refreshURL: '/msg/wechatTemplate/refresh',
      mixinViewModuleOptions: {
        activatedIsNeed: true,
        getDataListURL: '/msg/wechatTemplate/page',
        getDataListIsPage: true
      },
      dataForm: {
        accountId: '',
        title: ''
      }
    }
  },
  components: {
  },
  created () {
    this.searchEvent()
  },
  methods: {
    searchEvent () {
      this.page = 1
      this.getDataList()
    },
    syncEvent () {
      this.refresh().then(() => {
        this.searchEvent()
      })
    },
    refresh () {
      this.dataListLoading = true
      return new Promise((resolve, reject) => {
        this.$http.get(this.refreshURL).then(({ data: res }) => {
          this.dataListLoading = false
          if (res.code !== 0) {
            this.$message.error(res.msg)
            reject(new Error(false))
          } else {
            resolve(true)
          }
        }).catch(() => {
          this.dataListLoading = false
          reject(new Error(false))
        })
      })
    },
    cellDblclick ({ row }) {
      this.$emit('backFun', row)
    },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.searchEvent()
      })
    }
  }
}
</script>
